﻿#include "TcpCodec_Script.h"

#include "TcpCodec.h"
#include "ScriptInstructionFactory.h"
//#include "ScriptInstruction.h"

TcpCodec_Script::TcpCodec_Script()
{

}

TcpCodec_Script::~TcpCodec_Script()
{

}

void TcpCodec_Script::decode(DataPackage_TcpCodec* dataPackageInfo_User)
{
  QByteArray data = dataPackageInfo_User->getData();
  char* src=data.data();
  int size=data.size();
  int offset=0;
  uint type=*(uint*)(src+offset);
  offset+=sizeof (type);
  switch (type)
    {
    case GetScript:
      emit signal_script(ScriptInstructionFactory::fromByteArray(src,size,&offset));
      break;
    case SetScript:
      break;
    case StartScript:
      break;
    case StopScript:
      break;
    }
}

void TcpCodec_Script::getScript(uint deviceId)
{
  uint type=GetScript;
  tcpCodec->code(TcpCodecBase::Script,QByteArray((char*)&type,sizeof (type)).append((char*)&deviceId,sizeof (deviceId)));
}

void TcpCodec_Script::setScript(QList<ScriptInstruction*> scriptList)
{
  uint type=SetScript;
  tcpCodec->code(TcpCodecBase::Script,QByteArray((char*)&type,sizeof (type)).append(ScriptInstructionFactory::scriptToByteArray(scriptList)));
}

void TcpCodec_Script::startScript(uint deviceId)
{
  uint type=StartScript;
  tcpCodec->code(TcpCodecBase::Script,QByteArray((char*)&type,sizeof (type)).append((char*)&deviceId,sizeof (deviceId)));
}

void TcpCodec_Script::stopScript(uint deviceId)
{
  uint type=StopScript;
  tcpCodec->code(TcpCodecBase::Script,QByteArray((char*)&type,sizeof (type)).append((char*)&deviceId,sizeof (deviceId)));
}

TcpCodec* TcpCodec_Script::getTcpCodec()const
{
  return tcpCodec;
}

TcpCodec_Script* TcpCodec_Script::setTcpCodec(TcpCodec*const tcpCodec)
{
  this->tcpCodec=tcpCodec;
  this->tcpCodec->addToCodecMap(TcpCodecBase::Script,this);
  printInjection(this,tcpCodec);
  return this;
}

void TcpCodec_Script::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setTcpCodec(getObjFromMap<TcpCodec>(objMap));
}
